<?php
/**
 * 预约管理控制器
 *
 * @package    app
 * @version    1.0
 * @author     Ray 33705910@qq.com
 * @license    MIT License
 * @copyright  2013 - 2015 Ray
 * @link       http://wangxiaolei.cn
 */

/**
 * 本控制器主要用于：
 * 1.
 * @package  app
 * @extends  Controller
 */
namespace admin;

class Controller_Preset extends Controller_BaseController {


    public function before(){
        parent::before();

        $params = array(
            'controller_name' => '预约管理'
        );
        \View::set_global($params);
    }

    /**
    * 资讯列表
    *
    * @param $cid 资讯类目ID
    * @return
    */
    public function action_index(){
        $params = array(
            'title' => '预约管理',
            'menu' => 'preset',
            'action_name' => '所有预约'
        );

        $data = \Input::get();

        $items = \Model_Order::query()
            ->related(array('buyer'))
            ->where('from_id', \Session::get('seller')->id)
            ->where('order_type', 'BOOK');

        if(isset($data['start_at']) && $data['start_at']){
            $items->where('created_at', 'between', array(strtotime(\Input::get('start_at')), strtotime(\Input::get('end_at'))));
            unset($data['start_at']);
        }

        /*foreach ($data as $key => $value) {
        	$items->where($key, $value);
        }*/


        $items->order_by('created_at', 'desc');
        //分页查询
        $count = $items->count();
        $config = array(
            'pagination_url' => "/admin/preset",
            'total_items'    => $count,
            'per_page'       => \Input::get('count', 10),
            'uri_segment'    => 'start',
            'show_first'     => true,
            'show_last'      => true,
            'name'           => 'bootstrap3_cn'
        );

        $pagination = new \Pagination($config);
        $params['pagination'] = $pagination;        
        $params['items'] = $items
                            ->rows_offset($pagination->offset)
                            ->rows_limit($pagination->per_page)
                            ->get();

        \View::set_global($params);
        $this->template->content = \View::forge("ace/preset/index");
    }

  	/**
  	* 新增一条资讯
  	*
    * @param $id 资讯ID
  	* @return
  	*/
  	public function action_save($id = 0){

        $title = ($id ? "编辑" : "新增") . (\Input::get('action', false) == 'car_feature' ? "车型特色" : "资讯");
        $params = array(
            'title' => $title,
            'menu' => \Input::get('action', false) == 'car_feature' ? "car-feature" : "article-details",
            'action_name' => $title
        );

        $node = false;
        if($id){
            $node = \Model_Node::find($id);
        }

    	if(\Input::method() == 'POST'){
            $data = \Input::post();
            if( ! $data['title'] || ! $data['body']){
                \Session::set_flash('msg', array('status' => 'err', 'msg' => '请填写标题及内容', 'errcode' => 10));
            }else{
                if($node){
                    $node->set($data);
                }else{
                    $data['status'] = 0;
                    $data['is_delete'] = 0;
                    $data['user_ip'] = \Input::real_ip();
                    $data['user_agent'] = \Input::user_agent();
                    $node = \Model_Node::forge($data);
                }

                if($node->save()){
                    if(\Input::is_ajax()){
                        die(json_encode(array('status' => 'succ', 'msg' => '操作成功', 'errcode' => 0)));
                    }
                    \Session::set_flash('msg', array('status' => 'succ', 'msg' => '操作成功', 'errcode' => 0));
                }else{
                    if(\Input::is_ajax()){
                        die(json_encode(array('status' => 'err', 'msg' => '操作失败', 'errcode' => 20)));
                    }
                    \Session::set_flash('msg', array('status' => 'err', 'msg' => '操作失败', 'errcode' => 20));
                }
            }
            /*$val = \Validation::forge('MyRules');
            $val->add_callable('MyRules');
            $val->add_field( '标题', 'title', 'required');
            $val->add_field( '资讯详情', 'body', 'required');

            if (! $val->run()){
                foreach ($val->error() as $key => $value) {
                    $errors[$key] = (string)$value;
                }
                if(\Input::is_ajax()){
                    die(json_encode(array('status' => 'err', 'msg' => '表单验证错误', 'data' => $errors, 'errcode' => 10)));
                }
                \Session::set_flash('msg', array('status' => 'err', 'msg' => '表单验证错误', 'data' => $errors, 'errcode' => 10));
            }else{
                
            }*/
            
    	}

        if($node){
            $params['item'] = $node;
        }

        \View::set_global($params);
        $this->template->content = \View::forge("ace/article/details");
  	}

    /**
    * 资讯类目
    *
    * @param $id 资讯类目ID
    * @return
    */
    public function action_category($id = 0){
        $params = array(
            'title' => '资讯类目管理',
            'menu' => 'article-category'
        );
        
        \View::set_global($params);
        $this->template->content = \View::forge("ace/article/details");
    }

  	/**
  	* 删除一条资讯
  	*
  	* @param $id int 资讯ID
  	* @return
  	*/
  	public function action_delete($id){
        if( ! $id){
            die('无效的参数');
        }

        $node = \Model_Node::find($id);
        if( ! $node){
            die('无效的参数');
        }

        $node->is_delete = 1;
        if($node->save()){
            if($node->category_id == 1){
                \Response::redirect("/admin/node/");
            }else{
                \Response::redirect("/admin/node/list/teacher");
            }
        }
  	}

}
